/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       hero_events.cpp

	$Header: /game/hero_events.cpp $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "hero_events.h"

#include <algorithm>

#include "enum_operations.h"
#include "ownable_event.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

// --------------------------------------------------------------------------
// Unnamed namespace
// --------------------------------------------------------------------------

namespace
{

	// --------------------------------------------------------------------------
	// --------------------------------------------------------------------------
	template < typename t_event_list >
	bool read_events( std::streambuf & stream, t_event_list & events )
	{
		typedef typename t_event_list::value_type	t_event_ptr;
		typedef typename t_event_ptr::element_type	t_event;

		int count = get< t_uint16 >( stream );

		t_event_list new_events;
		new_events.reserve( count );

		while ( count > 0 )
		{
			t_event_ptr new_event_ptr( new t_event );
			if ( !new_event_ptr->read_from_map( stream ) )
				return false;

			new_events.push_back( new_event_ptr );

			--count;
		}

		events.swap( new_events );
		return true;
	}

} // Unnamed namespace

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool read_hero_built_in_events_from_map(
	std::streambuf &				stream,
	t_hero_built_in_event_list &	events )
{
	t_hero_built_in_event_id event_id;
	for (	event_id = t_hero_built_in_event_id( 0 );
			event_id < k_hero_built_in_event_count;
			enum_incr( event_id ) )
	{
		t_hero_built_in_event_ptr new_event_ptr( new t_hero_built_in_event );
		if ( !new_event_ptr->read_from_map( stream ) )
			return false;

		events[ event_id ] = new_event_ptr;
	}
	
	return true;
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool read_hero_timed_events_from_map(
	std::streambuf &			stream,
	t_hero_timed_event_list &	events )
{
	return read_events( stream, events );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool read_hero_triggerable_events_from_map(
	std::streambuf &				stream,
	t_hero_triggerable_event_list &	events )
{
	return read_events( stream, events );
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
bool read_hero_continuous_events_from_map(
	std::streambuf &				stream,
	t_hero_continuous_event_list &	events )
{
	return read_events( stream, events );
}
